<cfset BASE = CreateObject("component", "#session.rootDir#.cfc.base") />
<cfset mainQueries = CreateObject("component", "#session.rootDir#.cfc.queries") />
<cfset termQueries = CreateObject("component", "#session.rootDir#.cfc.terminationQueries") />
<cfset statusQueries = CreateObject("component", "#session.rootDir#.cfc.statusQueries") />
<cfset term = CreateObject("component", "#session.rootDir#.cfc.Termination") />
<cfinclude template="/code_lib/SCF_Employee.cfm">
<cfinclude template="/code_lib/SCF_HR.cfm">
<cfinclude template="/code_lib/SCF_REGEX.cfm">
<cfoutput>
<!---HTML header, css links, js links, and page title--->
#BASE.htmlTerminationHeader(
    "", <!---JS links--->
    "", <!---CSS links--->
    "Step 3: Scheduled Employee Termination", <!---Page Title--->
    "Status" <!---Active Nav Link--->
)#
<!---HTML header, css links, js links, and page title--->

<cfset SysErrorInstructions = "">
<cfset Step = "" />
<cfset Error = "" />      
<cfif (Session.bolAuth EQ True and (Session.strAuthType EQ Session.IT OR Session.strAuthType EQ Session.HR OR Session.strUserID EQ Session.SERVICE_ACCOUNT))>	

    <cfset QUERIES.GetSysScheduledTerms = termQueries.funcSysScheduledTerms( DateFormat(now(), "mm/dd/yyyy") ) />
    
	<cfset SysSummary = 'HR Scheduled Archive.  <BR/>Started:  #dateFormat(Now(), "mm/dd/yyyy")# #TimeFormat(Now(), "hh:mm:ss tt")#<BR /><hr /><br />'  >
    
    <cfloop query="QUERIES.GetSysScheduledTerms">        
        <cfset QUERIES.GetTermDetails = mainQueries.funcGetTermDetails( QUERIES.GetSysScheduledTerms.TERMREQID, 23 ) />
        <cfset QUERIES.GetEmpInfoByStep = mainQueries.funcGetEmpInfoTermination( QUERIES.GetTermDetails.ActionStepID ) />
        <cfset objEmp = SCF_Employee_GetEmployee(QUERIES.GetTermDetails.RosettaID) />
        <cfset objRequestor = SCF_Employee_GetEmployee(QUERIES.GetTermDetails.InitialTermReqByID) />
        <cfset SupID = SCF_HR_WhosTheBoss(QUERIES.GetTermDetails.RosettaID) />
        <cfset objSup = SCF_Employee_GetEmployee(SupID) />
        <cfset SysSummary &= 'User:  #objSup.FullName# (#objEmp.ExtendedLogin#)<BR/> Started: #dateFormat(Now(), "mm/dd/yyyy")# #TimeFormat(Now(), "hh:mm:ss tt")#<BR/>' />
        <cftry>
        <cfsetting enablecfoutputonly="true">
        <cfset Termination = term.init(QUERIES.GetSysScheduledTerms.TermReqID) />
        
        <!---1) Kronos HR Disable --->    
        <cfset Step = "Step 1 - HR Disable" />
        <cfset HRTerm = Termination.HRDisable() />
        <cfset HRResult = HRTerm.Message />
        <cfif HRTerm.STATUS EQ "Completed" >            
            <cfset HRResult = HRTerm.Message />
        <cfelse>         
            <cfset SysErrorInstructions &= "Error diabling Kronos HR record for  #objSup.FullName# (#objEmp.ExtendedLogin#)<Br /><Br />
	            <PRE>
	            Please take the following steps: <BR />
	            1) Contact HR and have them manually disable this employee <Br />
	            Error Text:  #HRResult# <BR/><BR/>
	            </PRE><hr />" 
            />                    
        </cfif>                                     
        <!---END 1 - Kronos HR Disable --->
        
        <!---4) Kronos TimeKeeper UserID Rename --->    
        <cfset Step = "Step 2 - Kronos TimeKeeper UserID Rename" />
        <cfset TKTerm = Termination.TimeKeeperDisable() />
        <cfset TKResult = TKTerm.Message />
        <cfif TKTerm.STATUS EQ "Completed" >            
            <cfset TKResult = TKTerm.Message />
        <cfelse>         
            <cfset SysErrorInstructions &= "Error renaming TimeKeeper record for  #objSup.FullName# (#objEmp.ExtendedLogin#)<Br /><Br />
	            <PRE>
	            Please take the following steps: <BR />
	            1) Contact HR or App Dev to disable TimeKeeper record <Br />
	            Error Text:  #TKResult# <BR/><BR/>
	            </PRE><hr />" 
            />   
        </cfif>                                     
        <!---END 4 - Kronos TimeKeeper UserID Rename --->
        
        <!---Final Clean up and Advance  --->
        
        <cfset mainQueries.funcUpdateAction( QUERIES.GetTermDetails.ActionStepID, -19, 'Scheduled Executed' ) />
        <cfset mainQueries.funcInsertAction("", 24, "", "", "", QUERIES.GetSysScheduledTerms.TERMREQID ) />
        
        <cfset SysSummary &= 'Results:<PRE> HR:  #HRResult# TimeKeeper:  #TKResult#</PRE>'/>        
        <cfset SysSummary &= 'Completed: #dateFormat(Now(), "mm/dd/yyyy")# #TimeFormat(Now(), "hh:mm:ss tt")#<BR /><hr /><br />'>
        <cfset termQueries.funcUpdateScheduledTerms( QUERIES.GetSysScheduledTerms.ID ) />
        
        <cfcatch type="any">
                <cfset Error &= ' Message: #CFCATCH.message#<BR />Detail: #CFCATCH.Detail#'>                
                <cfset mainQueries.funcInsertErrorLog(QUERIES.GetTermDetails.ActionStepID, -19, 'Error in Scheduled Termination/Layoff #Step#: #Error#')/>                
                <cfset SysSummary &= 'Error:  #Error#<BR/> Step: #Step#<BR /> Other Instructions: #SysErrorInstructions#<BR />'>                    
                <cfmail to=#Session.SERVICEDESK_EMAIL# From="EPAR_Error@scf.cc" subject="EPAR Scheduled Termination Error: #objEmp.ExtendedLogin#" type="html">
                    Error in Scheduled Termination/Layoff #Step#<BR /> 
                    Error Text: #Error#<BR /> 
                    Employee: #objEmp.ExtendedLogin#<BR /> 
                    Step: #Step#<BR />
                    Action by User: Service account (scheduled)
                </cfmail>
            </cfcatch>        
        </cftry>
    </cfloop>
    
    <cfif SysErrorInstructions NEQ "">
        <cfmail to=#Session.SERVICEDESK_EMAIL# From="EPAR_Error@scf.cc" subject="EPAR HR Scheduled Termination Error(s)" type="html">
            #SysErrorInstructions#
        </cfmail>    
        #SysErrorInstructions#
        <br /><br />        
    <cfelse>         
        #SysSummary# 
        <cfmail to=#Session.ITS_EMAIL# From="EPAR_Scheduled@scf.cc" subject="EPAR HR Scheduled Termination Summary" type="html">
            #SysSummary#
        </cfmail>              
    </cfif>
 
<cfelse> <!--- Authorization Check --->
	<cfinclude template="../Denied.cfm">	
</cfif>


<!---HTML footer, closes html and body tags.  Any footer info goes here--->
#BASE.htmlTerminationFooter()#
<!---HTML footer, closes html and body tags.  Any footer info goes here--->
</cfoutput>